<!-- 复位设备 -->
<template>
  <el-dialog title="复位设备" width="350" :model-value="modelValue" @close="commEvent.onClose">
    <el-form ref="formRef" :model="forms" :rules="rules">
      <el-form-item prop="mock">
        <el-select v-model="forms.mock" placeholder="请选择要复位的设备" clearable>
          <el-option v-for="item in deviceOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="commEvent.onClose">取消</el-button>
      <el-button type="primary" @click="commEvent.onSubmit">确认</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, watch, nextTick } from "vue"
import { FormInstance, FormRules } from "element-plus"
import { ElMessage } from "element-plus"
import { mockSave } from "@/api/modules/comm"

const props = defineProps({
  modelValue: { type: Boolean, default: false }
})
const emit = defineEmits(["update:modelValue", "refresh"])
// 参数
const formRef = ref<FormInstance>() // 表单实例
const deviceOptions = [
  { label: "配液设备", value: "01" },
  { label: "喷涂设备", value: "02" },
  { label: "烧结设备", value: "03" },
  { label: "XRF设备", value: "04" },
  { label: "组装+电化学检测设备", value: "05" }
]
// 表单参数
const forms = ref({ mock: "" })
// 表单校验
const rules: FormRules = {
  mock: [{ required: true, message: "请选择要复位的设备", trigger: "change" }]
}
// 事件
const commEvent = {
  // 关闭弹窗
  onClose: () => {
    emit("update:modelValue", false)
  },
  // 提交
  onSubmit: () => {
    if (!formRef.value) return
    formRef.value.validate(async valid => {
      if (!valid) return
      const { code } = await mockSave()
      if (code !== "200") return
      ElMessage.success("操作成功")
      commEvent.onClose()
    })
  }
}
watch(
  () => props.modelValue,
  val => {
    if (val) {
      nextTick(() => {
        if (!formRef.value) return
        formRef.value.resetFields()
      })
    }
  }
)
</script>

<style lang="scss" scoped></style>
